﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Traceability.App.API.Application.Command.Img;
using Traceability.App.API.Application.Command.Immune;
using Traceability.App.API.DTOS;
using Traceability.App.ErrorCode;

namespace Traceability.App.API.Controllers
{
    /// <summary>
    /// 图片上传控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ImgController : ControllerBase
    {

        IMediator mediator;

        public ImgController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Photo(IFormFile file)
        {
            try
            {
                string t = file.ContentType;
                //List<string> list = new List<string>
                //{
                //    "image/jpeg",
                //    "image/png",
                //    "image/gif",
                //    "image/webp"
                //};
                //if (!list.Contains(t))
                //{
                //    return Ok("不是图片");
                //}
                //var max = 1024 * 1024 * 2;
                //if (file.Length > max)
                //{
                //    return Ok("图片大于2M");
                //}
                string path = Directory.GetCurrentDirectory() + "/wwwroot/Imgs/" + file.FileName;
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                return Ok("https://localhost:7008/Imgs/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 创建图片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateImg(CreateImgCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 创建图片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreatePicture(CreateImgCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<ApiResult<int>> DeletePicture([FromQuery]DeleteImgCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据生产批次获取图片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<PictureDTO>>> GetPicture([FromQuery] GetPictureCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }


    }
}
